package com.happy3w.ssp.schoolmatehome.controller;

import com.happy3w.ssp.schoolmatehome.entity.ClockInTemplate;
import com.happy3w.ssp.schoolmatehome.interceptor.AttributeKeys;
import com.happy3w.ssp.schoolmatehome.service.auth.TheUserDetail;
import com.happy3w.ssp.schoolmatehome.service.template.ClockInTemplateService;
import com.happy3w.ssp.schoolmatehome.service.template.ClockInTemplateSummary;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/clock-in-templates")
public class ClockInTemplateController {
    private final ClockInTemplateService clockInTemplateService;

    @PostMapping
    public ResponseEntity<ClockInTemplate> create(@RequestBody ClockInTemplate template,
                                                  @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        ClockInTemplate createdTemplate = clockInTemplateService.create(template, userDetail.getUser().getWxUnionId());
        return ResponseEntity.ok(createdTemplate);
    }

    @GetMapping
    public ResponseEntity<List<ClockInTemplateSummary>> findAll(@RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        List<ClockInTemplateSummary> templates =
                clockInTemplateService.findAllByOwner(userDetail.getUser().getWxUnionId());
        return ResponseEntity.ok(templates);
    }

    @GetMapping("/{id}/summary")
    public ResponseEntity<ClockInTemplateSummary> findSummaryById(@PathVariable Long id,
                                                                  @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        Optional<ClockInTemplateSummary> templateOpt = clockInTemplateService.findSummaryById(id,
                userDetail.getUser().getWxUnionId());
        return templateOpt.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @GetMapping("/{id}")
    public ResponseEntity<ClockInTemplate> findById(@PathVariable Long id,
                                                    @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        Optional<ClockInTemplate> templateOpt = clockInTemplateService.findById(id, userDetail.getUser().getWxUnionId());
        return templateOpt.map(ResponseEntity::ok)
                .orElseGet(() -> ResponseEntity.notFound().build());
    }

    @PostMapping("/{id}")
    public ResponseEntity<ClockInTemplate> update(@PathVariable Long id,
                                                  @RequestBody ClockInTemplate template,
                                                  @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        ClockInTemplate updatedTemplate = clockInTemplateService.update(id, template,
                userDetail.getUser().getWxUnionId());
        return ResponseEntity.ok(updatedTemplate);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> delete(@PathVariable Long id,
                                       @RequestAttribute(AttributeKeys.USER_DETAIL) TheUserDetail userDetail) {
        clockInTemplateService.delete(id, userDetail.getUser().getWxUnionId());
        return ResponseEntity.noContent().build();
    }
}